﻿using UnityEngine;
using System.Collections;

public class EPButton : ObjectBase {

    public enum State
    {
        Normal,
        Hover,
        Pressed,
        Disabled        
    }

    public enum ButtonKind
    {
        Off,
        OnOff,
        OnOffEnd,
        OffEnd,
        DontChange
    }

    public bool IsNeedButtonEffect = true;

    Transform m_On;
    Transform m_Off;
    Transform m_End;
    Transform m_Disable;

    ButtonKind m_ButtonKind = ButtonKind.OnOff;

    [System.NonSerialized]
    protected State mState = State.Normal;

    public string m_Sound = "UI_button";

    public bool isEnabled 
    { 
        get
        {
            return enabled; 
        } 
        set
        {
            enabled = value;
            SetDisable();
        } 
    }

    void Awake()
    {
        if (IsNeedButtonEffect)
            SetEpButton();
        else
            m_ButtonKind = ButtonKind.DontChange;
    }

	void SetEpButton()
    {
        m_On = transform.Find("on");
        m_Off = transform.Find("off");
        m_End = transform.Find("end");
        m_Disable = transform.Find("disable");

        if (m_Off == null)
        {
            m_ButtonKind = ButtonKind.DontChange;
            return;
        }

        if (m_End != null)
            m_End.gameObject.SetActive(false);

        if (m_On != null && m_Off != null && m_End != null)
            m_ButtonKind = ButtonKind.OnOffEnd;
        else if (m_On != null && m_Off != null)
            m_ButtonKind = ButtonKind.OnOff;
        else if (m_On != null && m_End != null)
            m_ButtonKind = ButtonKind.OffEnd;
        else
            m_ButtonKind = ButtonKind.Off;
    }

    void OnPress(bool isPressed)
    {
        if (isEnabled && UICamera.currentTouch != null)
        {           
            if (isPressed)
            {
                SetState(State.Pressed, false);                
            }
            else if (UICamera.currentTouch.current == gameObject)
            {
                if (UICamera.currentScheme == UICamera.ControlScheme.Controller)
                {
                    SetState(State.Hover, false);
                }
                else if (UICamera.currentScheme == UICamera.ControlScheme.Mouse && UICamera.hoveredObject == gameObject)
                {
                    SetState(State.Hover, false);
                }
                else SetState(State.Normal, false);

                SoundController.PlaySound(m_Sound);
            }
            else SetState(State.Normal, false);
        }        
    }

    void SetDisable()
    {     
        if (m_Off == null)      //m_Off는 반드시 있어야 함
            SetEpButton();

        if (m_Disable != null)
            m_Disable.gameObject.SetActive(!isEnabled);
        if (m_On != null)
            m_On.gameObject.SetActive(false);
        if (m_Off != null)
            m_Off.gameObject.SetActive(isEnabled);
        if (m_End != null)
            m_End.gameObject.SetActive(false);

        if (collider != null)
            collider.enabled = isEnabled;

        if (isEnabled)
            SetState(State.Normal, false);
    }

    protected virtual void SetState(State state, bool instant)
    {
        if (mState != state && enabled && m_ButtonKind != ButtonKind.DontChange)
        {
            mState = state;

            switch (mState)
            {
                case State.Hover:
                    {
                        switch (m_ButtonKind)
                        {
                            case ButtonKind.Off:
                            case ButtonKind.OnOff:
                            case ButtonKind.OnOffEnd:
                            case ButtonKind.OffEnd:
                                {
                                    if (m_On != null)
                                        m_On.gameObject.SetActive(false);
                                    if (m_Off != null)
                                        m_Off.gameObject.SetActive(true);
                                    if (m_End != null)
                                        m_End.gameObject.SetActive(false);
                                }
                                break;
                        }

                        ShowEndEffect();
                    }
                    break;
                case State.Pressed:
                    {
                        switch (m_ButtonKind)
                        {                            
                            case ButtonKind.OnOff:
                            case ButtonKind.OnOffEnd:                          
                                {
                                    if (m_On != null)
                                        m_On.gameObject.SetActive(true);
                                    if (m_Off != null)
                                        m_Off.gameObject.SetActive(false);
                                    if (m_End != null)
                                        m_End.gameObject.SetActive(false);
                                }
                                break;
                        }                        
                    }
                    break;
                case State.Disabled:
                    {
                        switch (m_ButtonKind)
                        {
                            case ButtonKind.Off:
                            case ButtonKind.OnOff:
                            case ButtonKind.OnOffEnd:
                            case ButtonKind.OffEnd:
                                {
                                    if (m_On != null)
                                        m_On.gameObject.SetActive(false);
                                    if (m_Off != null)
                                        m_Off.gameObject.SetActive(true);
                                    if (m_End != null)
                                        m_End.gameObject.SetActive(false);
                                }
                                break;
                        }
                    }
                    break;
                default:
                    {
                        switch (m_ButtonKind)
                        {
                            case ButtonKind.Off:
                            case ButtonKind.OnOff:
                            case ButtonKind.OnOffEnd:
                            case ButtonKind.OffEnd:
                                {
                                    if (m_On != null)
                                        m_On.gameObject.SetActive(false);
                                    if (m_Off != null)
                                        m_Off.gameObject.SetActive(true);
                                    if (m_End != null)
                                        m_End.gameObject.SetActive(false);
                                }
                                break;
                        }  
                    }
                    break;
            }
        }
    }

    void ShowEndEffect()
    {
        if(m_End != null)
        {
            m_End.gameObject.SetActive(false);
            m_End.gameObject.SetActive(true);
            StopCoroutine("HideEndEffect");
            StartCoroutine("HideEndEffect");
        }
    }

    IEnumerator HideEndEffect()
    {
        yield return new WaitForSeconds(1.0f);
        m_End.gameObject.SetActive(false);
    }

    void OnDisable()        // 코루틴 돌기전에 버튼 인엑티비티되면 파티클 ( end ) 끔 , 이후 다시 엑티브 됬을때 대출력 방지
    {
        if (m_End != null)
        {
            m_End.gameObject.SetActive(false);            
        }
    }
}
